# Dockerfile
FROM openjdk:8-jre-slim

# 设置环境变量
ENV MODE="standalone" \
    PREFER_HOST_MODE="hostname" \
    BASE_DIR="/home/nacos" \
    CLASSPATH=".:/home/nacos/conf:/home/nacos/lib/*" \
    CLUSTER_CONF="/home/nacos/conf/cluster.conf" \
    FUNCTION_MODE="all" \
    SERVER_PORT="8848" \
    NACOS_USER="nacos" \
    JVM_XMS="512m" \
    JVM_XMX="512m" \
    JVM_XMN="256m" \
    JVM_MS="32m" \
    JVM_MMS="80m" \
    NACOS_DEBUG="n" \
    TOMCAT_ACCESSLOG_ENABLED="false" \
    TIME_ZONE="Asia/Shanghai"

# 创建用户和目录
RUN groupadd -r ${NACOS_USER} \
    && useradd -r -g ${NACOS_USER} ${NACOS_USER} \
    && mkdir -p ${BASE_DIR}/logs \
    && mkdir -p ${BASE_DIR}/conf \
    && mkdir -p ${BASE_DIR}/init.d \
    && chown -R ${NACOS_USER}:${NACOS_USER} ${BASE_DIR}

# 复制nacos文件
COPY --chown=nacos:nacos distribution/target/nacos-server-*.tar.gz /tmp/nacos-server.tar.gz
RUN tar -xzf /tmp/nacos-server.tar.gz -C /home \
    && mv /home/nacos/target/nacos-server-*/* /home/nacos/ \
    && rm -rf /home/nacos/target \
    && rm -f /tmp/nacos-server.tar.gz

# 复制配置文件
COPY --chown=nacos:nacos conf/application.properties ${BASE_DIR}/conf/

# 设置工作目录
WORKDIR ${BASE_DIR}

# 暴露端口
EXPOSE ${SERVER_PORT}

# 设置时区
RUN ln -sf /usr/share/zoneinfo/${TIME_ZONE} /etc/localtime \
    && echo ${TIME_ZONE} > /etc/timezone

# 切换用户
USER ${NACOS_USER}

# 启动命令
ENTRYPOINT ["bin/docker-startup.sh"]
